/*******************************************************************************************************/
/*****************************加载必要的js，初始化单页应用全局变量和函数********************************/
/*******************************************************************************************************/
import '../lib/flexible'
import $ from 'n-zepto'
import '../lib/jquery.cookie'

window._hmt = window._hmt || [];
(function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?eaa860101e837ecb49ee5f37445804d7";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s)
})();
//window._hmt.push(['_setAutoPageview', false]);

let _wap_config = {
	api: "http://api.test.jiuxiulvxing.com/web.api",
    official:'http://www.test.jiuxiulvxing.com/',
	domainUrl: "0ce37dd6b927730161a1e559c2336d0a",
	imgUrl: "http://img.test.yimayholiday.com/v1/tfs/",
    linkUrl: 'http://s0.test.jiuxiulvxing.com/Test/Integralfission/dist/',
    outLinkUrl: 'http://m.test.jiuxiulvxing.com/',
	contpointersApi: 'http://test.log.jiuxiulvxing.com/newlogwithgzip.jsp',
    vertificationImageUrl:"http://act.secondtest.jiuxiulvxing.com/",
    wxshare:"http://weixin.test.jiuxiulvxing.com/jssdk/getconfig/46",
    wxsharedebug: true
},_linkUrl = window.location.href;

//预发
if (_linkUrl.indexOf("p.pre.jiuxiulvxing.com") != -1) {
	_wap_config = {
		api: "http://api.pre.jiuxiulvxing.com/web.api",
        official:'http://www.pre.jiuxiulvxing.com/',
		domainUrl: "0ce37dd6b927730161a1e559c2336d0a",
		imgUrl: "http://img.yimayholiday.com/v1/tfs/",
		linkUrl: 'http://p.pre.jiuxiulvxing.com/Integralfission/dist/',
        outLinkUrl: 'http://m.pre.jiuxiulvxing.com/',
		contpointersApi: 'http://log.jiuxiulvxing.com/newlogwithgzip.jsp',
        vertificationImageUrl:"http://act.pre.jiuxiulvxing.com/",
        wxshare:"http://wxapi.pre.jiuxiulvxing.com/jssdk/getconfig/47",
        wxsharedebug: true
	}
}

//线上
if (_linkUrl.indexOf("p.jiuxiulvxing.com") != -1) {
	_wap_config = {
		api: "http://api.jiuxiulvxing.com/web.api",
        official:'http://www.jiuxiulvxing.com/',
		domainUrl: "0ce37dd6b927730161a1e559c2336d0a",
		imgUrl: "http://img.yimayholiday.com/v1/tfs/",
		linkUrl: 'http://p.jiuxiulvxing.com/Integralfission/dist/',
        outLinkUrl: 'http://app.jiuxiulvxing.com/',
		contpointersApi: 'http://log.jiuxiulvxing.com/newlogwithgzip.jsp',
        vertificationImageUrl:"http://act.jiuxiulvxing.com/",
        wxshare:"http://wxapi.jiuxiulvxing.com/jssdk/getconfig/46",
        wxsharedebug: false
	}
}

//公共参数
_wap_config.sm = "md5";
_wap_config.aid = "23";
_wap_config.aid_app = "28";
_wap_config.aid_wx = "25";
_wap_config.domid = "1200";
_wap_config.ft = "json";
_wap_config.ch = {
	jxwap: "jxwap",
	weixin: "weixin",
	jxapp: "jxapp"
};
_wap_config.timeout = "10000";

window._wap_config=_wap_config;

window._linkUrl=_linkUrl;

window.activityCode='INTEGRAL_FISSION';

//初始化打点函数
window.statistics={
    monitors:function(action_and_params,activity_info,getch){
        var stamps=new Date().getTime(),auto_params=[],config=null;
        for(var key in action_and_params){
            var obj={
                "2001": key,
                "2002": "",
                "2003": stamps+"",
                "2004": action_and_params[key],
                "2005": "",
                "2006": $.cookie('_uid'),
                "2007": "",
                "2008": activity_info?activity_info:""
            };
            auto_params.push(obj);
        }
        
        config={
            "1001": "九休旅行",
            "1002": this.jx_getaid(),
            "1003": "",
            "1004": "",
            "1005": "",  
            "1006": "",
            "1007": "",
            "1008": this.ck_system(),
            "1009": this.ios_or_android(),
            "1010": "",
            "1011": getch?getch:this.jx_getch(),
            "1012": "",
            "1013": "",
            "1014": this.ck_browser(),
            "1015": "",
            "1016": "",
            "1017": auto_params,
            "1018": "1200"
        };
        //console.log(JSON.stringify(config));
        $.ajax({
            dataType : "jsonp",
			xhrFields: {
				withCredentials: true
			},
			crossDomain: true,
            url: _wap_config.contpointersApi,//记录数据请求地址
            data: {msg:encodeURI(JSON.stringify(config))},
            beforeSend : (xhr)=>{ 
            	var uuid_value=this.uuid();
		    	if(!$.cookie('jx_uv'))
                	$.cookie('jx_uv',uuid_value);
			}
        });
    },
    uuid :function(len, radix) {
	    var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
	    var uuid = [], i;
	    radix = radix || chars.length;
	    if (len) {
	      // Compact form
	      for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
	    } else {
	      // rfc4122, version 4 form
	      var r;
	      // rfc4122 requires these characters
	      uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
	      uuid[14] = '4';
	      // Fill in random data.  At i==19 set the high bits of clock sequence as
	      // per rfc4122, sec. 4.1.5
	      for (i = 0; i < 36; i++) {
	        if (!uuid[i]) {
	          r = 0 | Math.random()*16;
	          uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
	        }
	      }
	    }
	    return uuid.join('');
	},
    ck_system:function() {
        var bIsAndroid = navigator.userAgent.match(/android/i) == "android",isIphone = (navigator.platform.toLowerCase() == "iphone") ; 
        if (isIphone||bIsAndroid) return "MOBILE";
        var isWin = (navigator.platform.toLowerCase() == "win32") || (navigator.platform.toLowerCase() == "windows");
        var isMac = (navigator.platform.toLowerCase() == "mac68k") || (navigator.platform.toLowerCase() == "macppc") 
        || (navigator.platform.toLowerCase() == "macintosh") || (navigator.platform.toLowerCase() == "macintel");
        var isUnix = (navigator.platform.toLowerCase() == "x11") && !isWin && !isMac;
        var isLinux = (String(navigator.platform.toLowerCase()).indexOf("linux") > -1);
        if (isWin||isMac||isUnix||isLinux) return "PC";
        return "PAD"; 
    },
	is_injxapp : function(){
		var ua = window.navigator.userAgent.toLowerCase();
		return ua.match(/JiuXiuLvXing/i) == 'jiuxiulvxing';
	},
	is_wxbrowser : function(){
		var ua = window.navigator.userAgent.toLowerCase();
		return ua.match(/MicroMessenger/i) == 'micromessenger';
	},
	isNull : function(obj) {
		if(typeof obj == "object"){
			for ( var name in obj ) {
				return false;
			}
			return true;
		}
		return null == obj || typeof obj == "undefined" || "" == obj || obj.length == 0;
	},
	jx_getch : function(){
		var ch = $.cookie("jx_ch");
		if (!this.isNull(ch)) {
			return ch;
		}
		if (this.is_injxapp()) {
			return _wap_config.ch.jxapp;
		}
		if (this.is_wxbrowser()) {
			return _wap_config.ch.weixin;
		}
		return _wap_config.ch.jxwap;
	},
	jx_getaid:function() {
		if (this.is_wxbrowser()) {
			return _wap_config.aid_wx;
		}else if (this.is_injxapp()) {
            return _wap_config.ch.aid_app;
        }
		return _wap_config.aid;
	},
    ios_or_android:function() {
        var isIphone = (navigator.platform.toLowerCase() == "iphone") ; 
        if (isIphone) return "ios";
        var bIsAndroid = (navigator.platform.toLowerCase() == "android");
        if(bIsAndroid) return "android"; 
        return '';
    },
    ck_browser:function(){
        var userAgent = navigator.userAgent.toLowerCase();
        var isOpera = userAgent.indexOf("opera") > -1;
        if (isOpera) {
            return "Opera浏览器"
        }
        if (userAgent.indexOf("firefox") > -1) {
            return "Firefox浏览器";
        }
        if (userAgent.indexOf("chrome") > -1){
          return "Chrome浏览器";
         }
        if (userAgent.indexOf("safari") > -1) {
            return "Safari浏览器";
        }
        if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("msie") > -1 && !isOpera) {
            return "IE浏览器";
        } 
        if(userAgent.indexOf("micromessenger") > -1) {  
            return "微信内嵌浏览器";
        } 
        if(userAgent.indexOf(" qq") > -1) {  
            return "QQ内嵌浏览器";
        }  
    }
};
